﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BibliotecaProjeto.usuario;
using BibliotecaProjeto.atendente;
using BibliotecaProjeto.tipoPasse;
using BibliotecaProjeto.estacao;
using BibliotecaProjeto.bicicleta;
using System.Data.SqlClient;
using System.Data;


namespace BibliotecaProjeto.aluguel
{
    public class DadosAluguel: persistencia.ConexaoSqlServer
    { 
        #region Atributos
        private string instSql;
        #endregion

        #region Construtores
        /// <summary>
        /// Construtor da Classe
        /// </summary>
        #endregion
        public DadosAluguel() { }

        #region Métodos
        #region conexão com select
        public List<Aluguel> selectAluguel()
        {
            List<Aluguel> retorno = new List<Aluguel>();
            try
            {
               abrirConexao();
                SqlCommand cmd = new SqlCommand("SELECT codigo FROM aluguel ", conBD);
                SqlDataReader DbReader = cmd.ExecuteReader();
                

                while (DbReader.Read())
                {
                    Aluguel al = new Aluguel();
                    al.codigo = DbReader.GetInt32(DbReader.GetOrdinal("codigo"));
                    retorno.Add(al);
                }

                DbReader.Close();
                cmd.Dispose();
                fecharConexao();;
            }
            catch (Exception ex)
            {
                throw new Exception("Erro ao conectar e selecionar " + ex.Message);
            }
            return retorno;
        }
        #endregion


        public void inserir(Aluguel aluguel)
        {
            try
            {
                SqlCommand cmd = conBD.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "inserirAluguel";

                cmd.Parameters.Add(new SqlParameter("@dataIni", aluguel.dataIni));
                cmd.Parameters.Add(new SqlParameter("@dataFim", aluguel.dataFim));
                cmd.Parameters.Add(new SqlParameter("@horaIni", aluguel.horaIni));
                cmd.Parameters.Add(new SqlParameter("@horaFim", aluguel.horaFim));
                cmd.Parameters.Add(new SqlParameter("@estCod", aluguel.estCod));
                cmd.Parameters.Add(new SqlParameter("@bicCod", aluguel.bicCod));
                cmd.Parameters.Add(new SqlParameter("@usuCpf", aluguel.usuCpf));
                cmd.Parameters.Add(new SqlParameter("@atdMat", aluguel.atdMat));
                cmd.Parameters.Add(new SqlParameter("@tipPasseCod", aluguel.tipoPasseCod));

                cmd.Prepare();
                abrirConexao();
                cmd.ExecuteNonQuery();
                fecharConexao();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public void remover(Aluguel aluguel)
        {
            try
            {
               abrirConexao();
                instSql = "delete from aluguel where codigo = " + aluguel.codigo + "";
                fecharConexao();;
            }
            catch (Exception ex)
            {
                throw new Exception("Erro ao remover aluguel: " + ex.Message);
            }
        }

        #endregion
    }
}